package com.phoenix.rabbitmq.service.producer;

import com.phoenix.rabbitmq.config.RabbitMQConfig;
import com.phoenix.rabbitmq.pojo.OrderMessage;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class OrderProducerService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendOrderMessage(OrderMessage orderMessage) {
        String msgId = UUID.randomUUID().toString();
        CorrelationData correlationData = new CorrelationData(msgId);

        rabbitTemplate.convertAndSend(RabbitMQConfig.ORDER_EXCHANGE,RabbitMQConfig.ORDER_ROUTINGKEY,orderMessage,message -> {
            //设置消息持久化
            message.getMessageProperties().setCorrelationId(msgId);
            message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            message.getMessageProperties().setCorrelationId(msgId);
            return message;
        }, correlationData);

        System.out.println("📤 发送订单消息：" + orderMessage);
    }
}
